package com.example.prison.util;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.entity.AttendanceList;
import com.example.prison.entity.PeopleManage;
import com.example.prison.entity.VisitorAppointment;
import com.example.prison.service.AttendanceDeviceInfoService;
import com.example.prison.service.AttendanceListService;
import com.example.prison.service.PeopleManageService;
import com.example.prison.service.VisitorAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;

public class AttDeviceUtil {

    @Autowired
    private PeopleManageService peopleManageService;
    @Autowired
    private VisitorAppointmentService visitorAppointmentService;
    @Autowired
    private AttendanceDeviceInfoService attendanceDeviceInfoService;
    @Autowired
    private AttendanceListService attendanceListService;

    public String bingPeople(String SN, String id, String time, AttendanceList attendanceList) {
        String beginTime = time;
        String endTime = TimeUtil.minReduce(time,10);
        if(attendanceList.getPtype() == "访客" ) {
            if (ObjectUtils.isNotEmpty(visitorAppointmentService.getByPIDAndTime(id, time.substring(0, 10)))) {
                //此次考勤的访客数据
                VisitorAppointment visitor = visitorAppointmentService.getByPIDAndTime(id, time.substring(0, 10));
                if (ObjectUtils.isNotEmpty(attendanceListService.getBySNOrderByTime(SN, beginTime, endTime).getList().get(1))) {
                    //获取上一条10分钟之内的带领人的记录
                    AttendanceList latestAttend = attendanceListService.getBySNAndTimeAndPType(SN, beginTime, endTime, "内部人员");
                    //AttendanceList latestAttend = attendanceListService.getBySNOrderByTime(SN, beginTime, endTime).getList().get(1);
                    if (ObjectUtils.isNotEmpty(peopleManageService.getByPID(latestAttend.getId()))) {
                        //获取对应带领人信息
                        PeopleManage people = peopleManageService.getByPID(latestAttend.getId());
                        if (visitor.getVisitorStatus() == "0" && ObjectUtils.isEmpty(visitor.getReceptionist())) {
                            //绑定带路人，修改访客状态为已入场  0未进场/1已入场/2已通过监狱门岗检查/3已通过隔离仓检查/4已通过值班人员检查/5已出场
                            visitor.setVisitorStatus("1");
                            if(attendanceList.getAccess().contains("出")) {
                                visitor.setOutReceptionist(people.getName());
                            } else {
                                visitor.setReceptionist(people.getName());
                            }
                        }
                    }
                }
                visitorAppointmentService.update(visitor, visitor.getId());
            }
        }
        else {
            if (ObjectUtils.isNotEmpty(peopleManageService.getByPID(id))) {
                //此次考勤的人员数据
                PeopleManage people = peopleManageService.getByPID(id);
                if (ObjectUtils.isNotEmpty(attendanceListService.getBySNOrderByTime(SN, beginTime, endTime).getList().get(2))) {
                    //获取上一条10分钟之内的访客记录
                    AttendanceList latestAttend = attendanceListService.getBySNAndTimeAndPType(SN, beginTime, endTime, "访客");
                    //AttendanceList latestAttend = attendanceListService.getBySNOrderByTime(SN, beginTime, endTime).getList().get(2);
                    if ( ObjectUtils.isNotEmpty(visitorAppointmentService.getByPIDAndTime(latestAttend.getId(), time.substring(0, 10))) ) {
                        //获取对应访客人信息
                        VisitorAppointment visitor = visitorAppointmentService.getByPIDAndTime(latestAttend.getId(), time.substring(0, 10));
                        if ( visitor.getVisitorStatus() == "1" && (people.getPost().contains("哨")||people.getPost().contains("门")) ) {
                            //绑定监狱门岗人员，修改访客状态为2已通过监狱门岗检查  0未进场/1已入场/2已通过监狱门岗检查/3已通过隔离仓检查/4已通过值班人员检查/5已出场
                            visitor.setVisitorStatus("2");
                            if(attendanceList.getAccess().contains("出")) {
                                visitor.setOutGateSentry(people.getName());
                            } else {
                                visitor.setInGateSentry(people.getName());
                            }
                            //visitor.setInGateSentry(people.getName());
                        } else if ( visitor.getVisitorStatus() == "3" && people.getPost().contains("值班") ) {
                            //绑定值班人员，修改访客状态为4已通过值班人员检查  0未进场/1已入场/2已通过监狱门岗检查/3已通过隔离仓检查/4已通过值班人员检查/5已出场
                            visitor.setVisitorStatus("4");
                            if(attendanceList.getAccess().contains("出")) {
                                visitor.setOutDutyPolice(people.getName());
                            } else {
                                visitor.setInDutyPolice(people.getName());
                            }
                            //visitor.setInDutyPolice(people.getName());
                        }
                        visitorAppointmentService.update(visitor, visitor.getId());
                    }
                }
            }
        }

        return "OK";
    }

}
